<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Geometry &mdash; Python GDAL/OGR Cookbook 1.0 documentation</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Python GDAL/OGR Cookbook 1.0 documentation" href="index.html" />
    <link rel="next" title="Vector Layers" href="vector_layers.html" />
    <link rel="prev" title="GDAL/OGR General" href="gdal_general.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="vector_layers.html" title="Vector Layers"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="gdal_general.html" title="GDAL/OGR General"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Python GDAL/OGR Cookbook 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="geometry">
<h1>Geometry<a class="headerlink" href="#geometry" title="Permalink to this headline">¶</a></h1>
<div class="section" id="create-a-point">
<h2>Create a Point<a class="headerlink" href="#create-a-point" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">)</span>
<span class="n">point</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1198054.34</span><span class="p">,</span> <span class="mf">648493.09</span><span class="p">)</span>
<span class="k">print</span> <span class="n">point</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-linestring">
<h2>Create a LineString<a class="headerlink" href="#create-a-linestring" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLineString</span><span class="p">)</span>
<span class="n">line</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1116651.439379124</span><span class="p">,</span> <span class="mf">637392.6969887456</span><span class="p">)</span>
<span class="n">line</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1188804.0108498496</span><span class="p">,</span> <span class="mf">652655.7409537067</span><span class="p">)</span>
<span class="n">line</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1226730.3625203592</span><span class="p">,</span> <span class="mf">634155.0816022386</span><span class="p">)</span>
<span class="n">line</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1281307.30760719</span><span class="p">,</span> <span class="mf">636467.6640211721</span><span class="p">)</span>
<span class="k">print</span> <span class="n">line</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-polygon">
<h2>Create a Polygon<a class="headerlink" href="#create-a-polygon" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># Create ring</span>
<span class="n">ring</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1161053.0218226474</span><span class="p">,</span> <span class="mf">667456.2684348812</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1214704.933941905</span><span class="p">,</span> <span class="mf">641092.8288590391</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1228580.428455506</span><span class="p">,</span> <span class="mf">682719.3123998424</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1218405.0658121984</span><span class="p">,</span> <span class="mf">721108.1805541387</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>

<span class="c"># Create polygon</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring</span><span class="p">)</span>

<span class="k">print</span> <span class="n">poly</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-polygon-with-holes">
<h2>Create a Polygon with holes<a class="headerlink" href="#create-a-polygon-with-holes" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># Create outer ring</span>
<span class="n">outRing</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">outRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1154115.274565847</span><span class="p">,</span> <span class="mf">686419.4442701361</span><span class="p">)</span>
<span class="n">outRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1154115.274565847</span><span class="p">,</span> <span class="mf">653118.2574374934</span><span class="p">)</span>
<span class="n">outRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1165678.1866605144</span><span class="p">,</span> <span class="mf">653118.2574374934</span><span class="p">)</span>
<span class="n">outRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1165678.1866605144</span><span class="p">,</span> <span class="mf">686419.4442701361</span><span class="p">)</span>
<span class="n">outRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1154115.274565847</span><span class="p">,</span> <span class="mf">686419.4442701361</span><span class="p">)</span>

<span class="c"># Create inner ring</span>
<span class="n">innerRing</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">innerRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1149490.1097279799</span><span class="p">,</span> <span class="mf">691044.6091080031</span><span class="p">)</span>
<span class="n">innerRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1149490.1097279799</span><span class="p">,</span> <span class="mf">648030.5761158396</span><span class="p">)</span>
<span class="n">innerRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1191579.1097525698</span><span class="p">,</span> <span class="mf">648030.5761158396</span><span class="p">)</span>
<span class="n">innerRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1191579.1097525698</span><span class="p">,</span> <span class="mf">691044.6091080031</span><span class="p">)</span>
<span class="n">innerRing</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1149490.1097279799</span><span class="p">,</span> <span class="mf">691044.6091080031</span><span class="p">)</span>

<span class="c"># Create polygon</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">outRing</span><span class="p">)</span>
<span class="n">poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">innerRing</span><span class="p">)</span>

<span class="k">print</span> <span class="n">poly</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-multipoint">
<h2>Create a MultiPoint<a class="headerlink" href="#create-a-multipoint" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">multipoint</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbMultiPoint</span><span class="p">)</span>

<span class="n">point1</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">)</span>
<span class="n">point1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1251243.7361610543</span><span class="p">,</span> <span class="mf">598078.7958668759</span><span class="p">)</span>
<span class="n">multipoint</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">point1</span><span class="p">)</span>

<span class="n">point2</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">)</span>
<span class="n">point2</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1240605.8570339603</span><span class="p">,</span> <span class="mf">601778.9277371694</span><span class="p">)</span>
<span class="n">multipoint</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">point2</span><span class="p">)</span>

<span class="n">point3</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">)</span>
<span class="n">point3</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1250318.7031934808</span><span class="p">,</span> <span class="mf">606404.0925750365</span><span class="p">)</span>
<span class="n">multipoint</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">point3</span><span class="p">)</span>

<span class="k">print</span> <span class="n">multipoint</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-multilinestring">
<h2>Create a MultiLineString<a class="headerlink" href="#create-a-multilinestring" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">multiline</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbMultiLineString</span><span class="p">)</span>

<span class="n">line1</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLineString</span><span class="p">)</span>
<span class="n">line1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1214242.4174581182</span><span class="p">,</span> <span class="mf">617041.9717021306</span><span class="p">)</span>
<span class="n">line1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1234593.142744733</span><span class="p">,</span> <span class="mf">629529.9167643716</span><span class="p">)</span>
<span class="n">multiline</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">line1</span><span class="p">)</span>

<span class="n">line1</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLineString</span><span class="p">)</span>
<span class="n">line1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1184641.3624957693</span><span class="p">,</span> <span class="mf">626754.8178616514</span><span class="p">)</span>
<span class="n">line1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1219792.6152635587</span><span class="p">,</span> <span class="mf">606866.6090588232</span><span class="p">)</span>
<span class="n">multiline</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">line1</span><span class="p">)</span>

<span class="k">print</span> <span class="n">multiline</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-multipolygon">
<h2>Create a MultiPolygon<a class="headerlink" href="#create-a-multipolygon" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">multipolygon</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbMultiPolygon</span><span class="p">)</span>

<span class="c"># Create ring #1</span>
<span class="n">ring1</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ring1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1204067.0548148106</span><span class="p">,</span> <span class="mf">634617.5980860253</span><span class="p">)</span>
<span class="n">ring1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1204067.0548148106</span><span class="p">,</span> <span class="mf">620742.1035724243</span><span class="p">)</span>
<span class="n">ring1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1215167.4504256917</span><span class="p">,</span> <span class="mf">620742.1035724243</span><span class="p">)</span>
<span class="n">ring1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1215167.4504256917</span><span class="p">,</span> <span class="mf">634617.5980860253</span><span class="p">)</span>
<span class="n">ring1</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1204067.0548148106</span><span class="p">,</span> <span class="mf">634617.5980860253</span><span class="p">)</span>

<span class="c"># Create polygon #1</span>
<span class="n">poly1</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">poly1</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring1</span><span class="p">)</span>
<span class="n">multipolygon</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">poly1</span><span class="p">)</span>

<span class="c"># Create ring #2</span>
<span class="n">ring2</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ring2</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179553.6811741155</span><span class="p">,</span> <span class="mf">647105.5431482664</span><span class="p">)</span>
<span class="n">ring2</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179553.6811741155</span><span class="p">,</span> <span class="mf">626292.3013778647</span><span class="p">)</span>
<span class="n">ring2</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1194354.20865529</span><span class="p">,</span> <span class="mf">626292.3013778647</span><span class="p">)</span>
<span class="n">ring2</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1194354.20865529</span><span class="p">,</span> <span class="mf">647105.5431482664</span><span class="p">)</span>
<span class="n">ring2</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179553.6811741155</span><span class="p">,</span> <span class="mf">647105.5431482664</span><span class="p">)</span>

<span class="c"># Create polygon #2</span>
<span class="n">poly2</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">poly2</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring2</span><span class="p">)</span>
<span class="n">multipolygon</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">poly2</span><span class="p">)</span>

<span class="k">print</span> <span class="n">multipolygon</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-a-geometrycollection">
<h2>Create a GeometryCollection<a class="headerlink" href="#create-a-geometrycollection" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># Create a geometry collection</span>
<span class="n">geomcol</span> <span class="o">=</span>  <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbGeometryCollection</span><span class="p">)</span>

<span class="c"># Add a point</span>
<span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPoint</span><span class="p">)</span>
<span class="n">point</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="o">-</span><span class="mf">122.23</span><span class="p">,</span> <span class="mf">47.09</span><span class="p">)</span>
<span class="n">geomcol</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">point</span><span class="p">)</span>

<span class="c"># Add a line</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLineString</span><span class="p">)</span>
<span class="n">line</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="o">-</span><span class="mf">122.60</span><span class="p">,</span> <span class="mf">47.14</span><span class="p">)</span>
<span class="n">line</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="o">-</span><span class="mf">122.48</span><span class="p">,</span> <span class="mf">47.23</span><span class="p">)</span>
<span class="n">geomcol</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>

<span class="k">print</span> <span class="n">geomcol</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="create-geometry-from-wkt">
<h2>Create Geometry from WKT<a class="headerlink" href="#create-geometry-from-wkt" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;POINT (1120351.5712494177 741921.4223245403)&quot;</span>
<span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s">,</span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">point</span><span class="o">.</span><span class="n">GetX</span><span class="p">(),</span> <span class="n">point</span><span class="o">.</span><span class="n">GetY</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="create-geometry-from-geojson">
<h2>Create Geometry from GeoJSON<a class="headerlink" href="#create-geometry-from-geojson" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">geojson</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;{&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[108420.33,753808.59]}&quot;&quot;&quot;</span>
<span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromJson</span><span class="p">(</span><span class="n">geojson</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s">,</span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">point</span><span class="o">.</span><span class="n">GetX</span><span class="p">(),</span> <span class="n">point</span><span class="o">.</span><span class="n">GetY</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="create-geometry-from-gml">
<h2>Create Geometry from GML<a class="headerlink" href="#create-geometry-from-gml" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">gml</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;&lt;gml:Point xmlns:gml=&quot;http://www.opengis.net/gml&quot;&gt;&lt;gml:coordinates&gt;108420.33,753808.59&lt;/gml:coordinates&gt;&lt;/gml:Point&gt;&quot;&quot;&quot;</span>
<span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromGML</span><span class="p">(</span><span class="n">gml</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s">,</span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">point</span><span class="o">.</span><span class="n">GetX</span><span class="p">(),</span> <span class="n">point</span><span class="o">.</span><span class="n">GetY</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="create-geometry-from-wkb">
<h2>Create Geometry from WKB<a class="headerlink" href="#create-geometry-from-wkb" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="kn">from</span> <span class="nn">base64</span> <span class="kn">import</span> <span class="n">b64decode</span>

<span class="n">wkb</span> <span class="o">=</span> <span class="n">b64decode</span><span class="p">(</span><span class="s">&quot;AIAAAAFBMkfmVwo9cUEjylouFHrhAAAAAAAAAAA=&quot;</span><span class="p">)</span>
<span class="n">point</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkb</span><span class="p">(</span><span class="n">wkb</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;</span><span class="si">%d</span><span class="s">,</span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">point</span><span class="o">.</span><span class="n">GetX</span><span class="p">(),</span> <span class="n">point</span><span class="o">.</span><span class="n">GetY</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="count-points-in-a-geometry">
<h2>Count Points in a Geometry<a class="headerlink" href="#count-points-in-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;LINESTRING (1181866.263593049 615654.4222507705, 1205917.1207499576 623979.7189589312, 1227192.8790041457 643405.4112779726, 1224880.2965852122 665143.6860159477)&quot;</span>
<span class="n">geom</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Geometry has </span><span class="si">%i</span><span class="s"> points&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">geom</span><span class="o">.</span><span class="n">GetPointCount</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="count-geometries-in-a-geometry">
<h2>Count Geometries in a Geometry<a class="headerlink" href="#count-geometries-in-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;MULTIPOINT (1181866.263593049 615654.4222507705, 1205917.1207499576 623979.7189589312, 1227192.8790041457 643405.4112779726, 1224880.2965852122 665143.6860159477)&quot;</span>
<span class="n">geom</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Geometry has </span><span class="si">%i</span><span class="s"> geometries&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">geom</span><span class="o">.</span><span class="n">GetGeometryCount</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="iterate-over-geometries-in-a-geometry">
<h2>Iterate over Geometries in a Geometry<a class="headerlink" href="#iterate-over-geometries-in-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;MULTIPOINT (1181866.263593049 615654.4222507705, 1205917.1207499576 623979.7189589312, 1227192.8790041457 643405.4112779726, 1224880.2965852122 665143.6860159477)&quot;</span>
<span class="n">geom</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">geom</span><span class="o">.</span><span class="n">GetGeometryCount</span><span class="p">()):</span>
    <span class="n">g</span> <span class="o">=</span> <span class="n">geom</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&quot;</span><span class="si">%i</span><span class="s">). </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">g</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="iterate-over-points-in-a-geometry">
<h2>Iterate over Points in a Geometry<a class="headerlink" href="#iterate-over-points-in-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;LINESTRING (1181866.263593049 615654.4222507705, 1205917.1207499576 623979.7189589312, 1227192.8790041457 643405.4112779726, 1224880.2965852122 665143.6860159477)&quot;</span>
<span class="n">geom</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">geom</span><span class="o">.</span><span class="n">GetPointCount</span><span class="p">()):</span>
    <span class="c"># GetPoint returns a tuple not a Geometry</span>
    <span class="n">pt</span> <span class="o">=</span> <span class="n">geom</span><span class="o">.</span><span class="n">GetPoint</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&quot;</span><span class="si">%i</span><span class="s">). POINT (</span><span class="si">%d</span><span class="s"> </span><span class="si">%d</span><span class="s">)&quot;</span> <span class="o">%</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">pt</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">pt</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="buffer-a-geometry">
<h2>Buffer a Geometry<a class="headerlink" href="#buffer-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;POINT (1198054.34 648493.09)&quot;</span>
<span class="n">pt</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
<span class="n">bufferDistance</span> <span class="o">=</span> <span class="mi">500</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">pt</span><span class="o">.</span><span class="n">Buffer</span><span class="p">(</span><span class="n">bufferDistance</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> buffered by </span><span class="si">%d</span><span class="s"> is </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pt</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">(),</span> <span class="n">bufferDistance</span><span class="p">,</span> <span class="n">poly</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="calculate-envelope-of-a-geometry">
<h2>Calculate Envelope of a Geometry<a class="headerlink" href="#calculate-envelope-of-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;LINESTRING (1181866.263593049 615654.4222507705, 1205917.1207499576 623979.7189589312, 1227192.8790041457 643405.4112779726, 1224880.2965852122 665143.6860159477)&quot;</span>
<span class="n">geom</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
<span class="c"># Get Envelope returns a tuple (minX, maxX, minY, maxY)</span>
<span class="n">env</span> <span class="o">=</span> <span class="n">geom</span><span class="o">.</span><span class="n">GetEnvelope</span><span class="p">()</span>
<span class="k">print</span> <span class="s">&quot;minX: </span><span class="si">%d</span><span class="s">, minY: </span><span class="si">%d</span><span class="s">, maxX: </span><span class="si">%d</span><span class="s">, maxY: </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">env</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span><span class="n">env</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="n">env</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="calculate-the-area-of-a-geometry">
<h2>Calculate the Area of a Geometry<a class="headerlink" href="#calculate-the-area-of-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;POLYGON ((1162440.5712740074 672081.4332727483, 1162440.5712740074 647105.5431482664, 1195279.2416228633 647105.5431482664, 1195279.2416228633 672081.4332727483, 1162440.5712740074 672081.4332727483))&quot;</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Area = </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">poly</span><span class="o">.</span><span class="n">GetArea</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="calculate-the-length-of-a-geometry">
<h2>Calculate the Length of a Geometry<a class="headerlink" href="#calculate-the-length-of-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt</span> <span class="o">=</span> <span class="s">&quot;LINESTRING (1181866.263593049 615654.4222507705, 1205917.1207499576 623979.7189589312, 1227192.8790041457 643405.4112779726, 1224880.2965852122 665143.6860159477)&quot;</span>
<span class="n">geom</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;Length = </span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">geom</span><span class="o">.</span><span class="n">Length</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="get-the-geometry-type-as-a-string-from-a-geometry">
<h2>Get the geometry type (as a string) from a Geometry<a class="headerlink" href="#get-the-geometry-type-as-a-string-from-a-geometry" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkts</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s">&quot;POINT (1198054.34 648493.09)&quot;</span><span class="p">,</span>
    <span class="s">&quot;LINESTRING (1181866.263593049 615654.4222507705, 1205917.1207499576 623979.7189589312, 1227192.8790041457 643405.4112779726, 1224880.2965852122 665143.6860159477)&quot;</span><span class="p">,</span>
    <span class="s">&quot;POLYGON ((1162440.5712740074 672081.4332727483, 1162440.5712740074 647105.5431482664, 1195279.2416228633 647105.5431482664, 1195279.2416228633 672081.4332727483, 1162440.5712740074 672081.4332727483))&quot;</span>
<span class="p">]</span>

<span class="k">for</span> <span class="n">wkt</span> <span class="ow">in</span> <span class="n">wkts</span><span class="p">:</span>
    <span class="n">geom</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">)</span>
    <span class="k">print</span> <span class="n">geom</span><span class="o">.</span><span class="n">GetGeometryName</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="calculate-intersection-between-two-geometries">
<h2>Calculate intersection between two Geometries<a class="headerlink" href="#calculate-intersection-between-two-geometries" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt1</span> <span class="o">=</span> <span class="s">&quot;POLYGON ((1208064.271243039 624154.6783778917, 1208064.271243039 601260.9785661874, 1231345.9998651114 601260.9785661874, 1231345.9998651114 624154.6783778917, 1208064.271243039 624154.6783778917))&quot;</span>
<span class="n">wkt2</span> <span class="o">=</span> <span class="s">&quot;POLYGON ((1199915.6662253144 633079.3410163528, 1199915.6662253144 614453.958118695, 1219317.1067437078 614453.958118695, 1219317.1067437078 633079.3410163528, 1199915.6662253144 633079.3410163528)))&quot;</span>

<span class="n">poly1</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt1</span><span class="p">)</span>
<span class="n">poly2</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt2</span><span class="p">)</span>

<span class="n">intersection</span> <span class="o">=</span> <span class="n">poly1</span><span class="o">.</span><span class="n">Intersection</span><span class="p">(</span><span class="n">poly2</span><span class="p">)</span>

<span class="k">print</span> <span class="n">intersection</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="calculate-union-between-two-geometries">
<h2>Calculate union between two Geometries<a class="headerlink" href="#calculate-union-between-two-geometries" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="n">wkt1</span> <span class="o">=</span> <span class="s">&quot;POLYGON ((1208064.271243039 624154.6783778917, 1208064.271243039 601260.9785661874, 1231345.9998651114 601260.9785661874, 1231345.9998651114 624154.6783778917, 1208064.271243039 624154.6783778917))&quot;</span>
<span class="n">wkt2</span> <span class="o">=</span> <span class="s">&quot;POLYGON ((1199915.6662253144 633079.3410163528, 1199915.6662253144 614453.958118695, 1219317.1067437078 614453.958118695, 1219317.1067437078 633079.3410163528, 1199915.6662253144 633079.3410163528)))&quot;</span>

<span class="n">poly1</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt1</span><span class="p">)</span>
<span class="n">poly2</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">wkt2</span><span class="p">)</span>

<span class="n">union</span> <span class="o">=</span> <span class="n">poly1</span><span class="o">.</span><span class="n">Union</span><span class="p">(</span><span class="n">poly2</span><span class="p">)</span>

<span class="k">print</span> <span class="n">poly1</span>
<span class="k">print</span> <span class="n">poly2</span>
<span class="k">print</span> <span class="n">union</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="write-geometry-to-geojson">
<h2>Write Geometry to GeoJSON<a class="headerlink" href="#write-geometry-to-geojson" title="Permalink to this headline">¶</a></h2>
<p>There are two options to create a GeoJSON from a geometry.</p>
<p>You can either create a new GeoJSON file or simply export the geometry to Json and print it. Both options are explained below.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># Create test polygon</span>
<span class="n">ring</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1161053.0218226474</span><span class="p">,</span> <span class="mf">667456.2684348812</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1214704.933941905</span><span class="p">,</span> <span class="mf">641092.8288590391</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1228580.428455506</span><span class="p">,</span> <span class="mf">682719.3123998424</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1218405.0658121984</span><span class="p">,</span> <span class="mf">721108.1805541387</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring</span><span class="p">)</span>

<span class="c"># Create the output Driver</span>
<span class="n">outDriver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">&#39;GeoJSON&#39;</span><span class="p">)</span>

<span class="c"># Create the output GeoJSON</span>
<span class="n">outDataSource</span> <span class="o">=</span> <span class="n">outDriver</span><span class="o">.</span><span class="n">CreateDataSource</span><span class="p">(</span><span class="s">&#39;test.geojson&#39;</span><span class="p">)</span>
<span class="n">outLayer</span> <span class="o">=</span> <span class="n">outDataSource</span><span class="o">.</span><span class="n">CreateLayer</span><span class="p">(</span><span class="s">&#39;test.geojson&#39;</span><span class="p">,</span> <span class="n">geom_type</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span> <span class="p">)</span>

<span class="c"># Get the output Layer&#39;s Feature Definition</span>
<span class="n">featureDefn</span> <span class="o">=</span> <span class="n">outLayer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>

<span class="c"># create a new feature</span>
<span class="n">outFeature</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Feature</span><span class="p">(</span><span class="n">featureDefn</span><span class="p">)</span>

<span class="c"># Set new geometry</span>
<span class="n">outFeature</span><span class="o">.</span><span class="n">SetGeometry</span><span class="p">(</span><span class="n">poly</span><span class="p">)</span>

<span class="c"># Add new feature to output Layer</span>
<span class="n">outLayer</span><span class="o">.</span><span class="n">CreateFeature</span><span class="p">(</span><span class="n">outFeature</span><span class="p">)</span>

<span class="c"># dereference the feature</span>
<span class="n">outFeature</span> <span class="o">=</span> <span class="bp">None</span>

<span class="c"># Save and close DataSources</span>
<span class="n">outDataSource</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># Create test polygon</span>
<span class="n">ring</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1161053.0218226474</span><span class="p">,</span> <span class="mf">667456.2684348812</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1214704.933941905</span><span class="p">,</span> <span class="mf">641092.8288590391</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1228580.428455506</span><span class="p">,</span> <span class="mf">682719.3123998424</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1218405.0658121984</span><span class="p">,</span> <span class="mf">721108.1805541387</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring</span><span class="p">)</span>

<span class="n">geojson</span> <span class="o">=</span> <span class="n">poly</span><span class="o">.</span><span class="n">ExportToJson</span><span class="p">()</span>
<span class="k">print</span> <span class="n">geojson</span>
</pre></div>
</div>
</div>
<div class="section" id="write-geometry-to-wkt">
<h2>Write Geometry to WKT<a class="headerlink" href="#write-geometry-to-wkt" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># Create test polygon</span>
<span class="n">ring</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1161053.0218226474</span><span class="p">,</span> <span class="mf">667456.2684348812</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1214704.933941905</span><span class="p">,</span> <span class="mf">641092.8288590391</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1228580.428455506</span><span class="p">,</span> <span class="mf">682719.3123998424</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1218405.0658121984</span><span class="p">,</span> <span class="mf">721108.1805541387</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">geom_poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">geom_poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring</span><span class="p">)</span>

<span class="c"># Export geometry to WKT</span>
<span class="n">wkt</span> <span class="o">=</span> <span class="n">geom_poly</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
<span class="k">print</span> <span class="n">wkt</span>
</pre></div>
</div>
</div>
<div class="section" id="write-geometry-to-kml">
<h2>Write Geometry to KML<a class="headerlink" href="#write-geometry-to-kml" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># Create test polygon</span>
<span class="n">ring</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1161053.0218226474</span><span class="p">,</span> <span class="mf">667456.2684348812</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1214704.933941905</span><span class="p">,</span> <span class="mf">641092.8288590391</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1228580.428455506</span><span class="p">,</span> <span class="mf">682719.3123998424</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1218405.0658121984</span><span class="p">,</span> <span class="mf">721108.1805541387</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">geom_poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">geom_poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring</span><span class="p">)</span>

<span class="n">kml</span> <span class="o">=</span> <span class="n">geom_poly</span><span class="o">.</span><span class="n">ExportToKML</span><span class="p">()</span>
<span class="k">print</span> <span class="n">kml</span>
</pre></div>
</div>
</div>
<div class="section" id="write-geometry-to-wkb">
<h2>Write Geometry to WKB<a class="headerlink" href="#write-geometry-to-wkb" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># Create test polygon</span>
<span class="n">ring</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1161053.0218226474</span><span class="p">,</span> <span class="mf">667456.2684348812</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1214704.933941905</span><span class="p">,</span> <span class="mf">641092.8288590391</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1228580.428455506</span><span class="p">,</span> <span class="mf">682719.3123998424</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1218405.0658121984</span><span class="p">,</span> <span class="mf">721108.1805541387</span><span class="p">)</span>
<span class="n">ring</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="mf">1179091.1646903288</span><span class="p">,</span> <span class="mf">712782.8838459781</span><span class="p">)</span>
<span class="n">geom_poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">geom_poly</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ring</span><span class="p">)</span>

<span class="c"># Export geometry to WKT</span>
<span class="n">wkb</span> <span class="o">=</span> <span class="n">geom_poly</span><span class="o">.</span><span class="n">ExportToWkb</span><span class="p">()</span>
<span class="k">print</span> <span class="n">wkb</span>
</pre></div>
</div>
</div>
<div class="section" id="force-polygon-to-multipolygon">
<h2>Force polygon to multipolygon<a class="headerlink" href="#force-polygon-to-multipolygon" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>

<span class="c"># Given a test polygon</span>
<span class="n">poly_wkt</span><span class="o">=</span> <span class="s">&quot;POLYGON ((1179091.164690328761935 712782.883845978067257,1161053.021822647424415 667456.268434881232679,1214704.933941904921085 641092.828859039116651,1228580.428455505985767 682719.312399842427112,1218405.065812198445201 721108.180554138729349,1179091.164690328761935 712782.883845978067257))&quot;</span>
<span class="n">geom_poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">poly_wkt</span><span class="p">)</span>

<span class="c"># Force a polygon to multipolygon</span>
<span class="k">if</span> <span class="n">geom_poly</span><span class="o">.</span><span class="n">GetGeometryType</span><span class="p">()</span> <span class="o">==</span> <span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">:</span>
   <span class="n">geom_poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">ForceToMultiPolygon</span><span class="p">(</span><span class="n">geom_poly</span><span class="p">)</span>
   <span class="c"># if are iterating over features just to update the geometry</span>
   <span class="c"># to multipolygon you can update the geometry using &quot;feature.SetGeometryDirectly(geom_poly)&quot;</span>

<span class="c"># Then export geometry to WKT</span>
<span class="n">wkt</span> <span class="o">=</span> <span class="n">geom_poly</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
<span class="k">print</span> <span class="n">wkt</span>
</pre></div>
</div>
</div>
<div class="section" id="quarter-polygon-and-create-centroids">
<h2>Quarter polygon and create centroids<a class="headerlink" href="#quarter-polygon-and-create-centroids" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div>This recipe quarters a polygon and creates the centroid of the four quarters</div></blockquote>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ogr</span>

    <span class="c"># Given a test polygon</span>
<span class="n">poly_Wkt</span><span class="o">=</span> <span class="s">&quot;POLYGON((-107.42631019589980212 40.11971708125970082,-107.42455436683293613 40.12061219666851741,-107.42020981542387403 40.12004414402532859,-107.41789122063043749 40.12149008687303819,-107.41419947746419439 40.11811617239460048,-107.41915181585792993 40.11761695654455906,-107.41998470913324581 40.11894245264452508,-107.42203317637793702 40.1184088144647788,-107.42430674991324224 40.1174448122981957,-107.42430674991324224 40.1174448122981957,-107.42631019589980212 40.11971708125970082))&quot;</span>
<span class="n">geom_poly</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">CreateGeometryFromWkt</span><span class="p">(</span><span class="n">poly_Wkt</span><span class="p">)</span>
</pre></div>
</div>
<img alt="_images/quarter1.png" src="_images/quarter1.png" />
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Create 4 square polygons</span>
<span class="n">geom_poly_envelope</span> <span class="o">=</span> <span class="n">geom_poly</span><span class="o">.</span><span class="n">GetEnvelope</span><span class="p">()</span>
<span class="n">minX</span> <span class="o">=</span> <span class="n">geom_poly_envelope</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">minY</span> <span class="o">=</span> <span class="n">geom_poly_envelope</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="n">maxX</span> <span class="o">=</span> <span class="n">geom_poly_envelope</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">maxY</span> <span class="o">=</span> <span class="n">geom_poly_envelope</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="sd">coord0----coord1----coord2</span>
<span class="sd">|           |           |</span>
<span class="sd">coord3----coord4----coord5</span>
<span class="sd">|           |           |</span>
<span class="sd">coord6----coord7----coord8</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="n">coord0</span> <span class="o">=</span> <span class="n">minX</span><span class="p">,</span> <span class="n">maxY</span>
<span class="n">coord1</span> <span class="o">=</span> <span class="n">minX</span><span class="o">+</span><span class="p">(</span><span class="n">maxX</span><span class="o">-</span><span class="n">minX</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="n">maxY</span>
<span class="n">coord2</span> <span class="o">=</span> <span class="n">maxX</span><span class="p">,</span> <span class="n">maxY</span>
<span class="n">coord3</span> <span class="o">=</span> <span class="n">minX</span><span class="p">,</span> <span class="n">minY</span><span class="o">+</span><span class="p">(</span><span class="n">maxY</span><span class="o">-</span><span class="n">minY</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span>
<span class="n">coord4</span> <span class="o">=</span> <span class="n">minX</span><span class="o">+</span><span class="p">(</span><span class="n">maxX</span><span class="o">-</span><span class="n">minX</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="n">minY</span><span class="o">+</span><span class="p">(</span><span class="n">maxY</span><span class="o">-</span><span class="n">minY</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span>
<span class="n">coord5</span> <span class="o">=</span> <span class="n">maxX</span><span class="p">,</span> <span class="n">minY</span><span class="o">+</span><span class="p">(</span><span class="n">maxY</span><span class="o">-</span><span class="n">minY</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span>
<span class="n">coord6</span> <span class="o">=</span> <span class="n">minX</span><span class="p">,</span> <span class="n">minY</span>
<span class="n">coord7</span> <span class="o">=</span> <span class="n">minX</span><span class="o">+</span><span class="p">(</span><span class="n">maxX</span><span class="o">-</span><span class="n">minX</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="n">minY</span>
<span class="n">coord8</span> <span class="o">=</span> <span class="n">maxX</span><span class="p">,</span> <span class="n">minY</span>

<span class="n">ringTopLeft</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ringTopLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord0</span><span class="p">)</span>
<span class="n">ringTopLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord1</span><span class="p">)</span>
<span class="n">ringTopLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord4</span><span class="p">)</span>
<span class="n">ringTopLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord3</span><span class="p">)</span>
<span class="n">ringTopLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord0</span><span class="p">)</span>
<span class="n">polyTopLeft</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">polyTopLeft</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ringTopLeft</span><span class="p">)</span>


<span class="n">ringTopRight</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ringTopRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord1</span><span class="p">)</span>
<span class="n">ringTopRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord2</span><span class="p">)</span>
<span class="n">ringTopRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord5</span><span class="p">)</span>
<span class="n">ringTopRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord4</span><span class="p">)</span>
<span class="n">ringTopRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord1</span><span class="p">)</span>
<span class="n">polyTopRight</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">polyTopRight</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ringTopRight</span><span class="p">)</span>


<span class="n">ringBottomLeft</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ringBottomLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord3</span><span class="p">)</span>
<span class="n">ringBottomLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord4</span><span class="p">)</span>
<span class="n">ringBottomLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord7</span><span class="p">)</span>
<span class="n">ringBottomLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord6</span><span class="p">)</span>
<span class="n">ringBottomLeft</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord3</span><span class="p">)</span>
<span class="n">polyBottomLeft</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">polyBottomLeft</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ringBottomLeft</span><span class="p">)</span>


<span class="n">ringBottomRight</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLinearRing</span><span class="p">)</span>
<span class="n">ringBottomRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord4</span><span class="p">)</span>
<span class="n">ringBottomRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord5</span><span class="p">)</span>
<span class="n">ringBottomRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord8</span><span class="p">)</span>
<span class="n">ringBottomRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord7</span><span class="p">)</span>
<span class="n">ringBottomRight</span><span class="o">.</span><span class="n">AddPoint_2D</span><span class="p">(</span><span class="o">*</span><span class="n">coord4</span><span class="p">)</span>
<span class="n">polyBottomRight</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbPolygon</span><span class="p">)</span>
<span class="n">polyBottomRight</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">ringBottomRight</span><span class="p">)</span>
</pre></div>
</div>
<img alt="_images/quarter4.png" src="_images/quarter4.png" />
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Intersect 4 squares polygons with test polygon</span>
<span class="n">quaterPolyTopLeft</span> <span class="o">=</span> <span class="n">polyTopLeft</span><span class="o">.</span><span class="n">Intersection</span><span class="p">(</span><span class="n">geom_poly</span><span class="p">)</span>
<span class="n">quaterPolyTopRight</span> <span class="o">=</span>  <span class="n">polyTopRight</span><span class="o">.</span><span class="n">Intersection</span><span class="p">(</span><span class="n">geom_poly</span><span class="p">)</span>
<span class="n">quaterPolyBottomLeft</span> <span class="o">=</span>  <span class="n">polyBottomLeft</span><span class="o">.</span><span class="n">Intersection</span><span class="p">(</span><span class="n">geom_poly</span><span class="p">)</span>
<span class="n">quaterPolyBottomRight</span> <span class="o">=</span>  <span class="n">polyBottomRight</span><span class="o">.</span><span class="n">Intersection</span><span class="p">(</span><span class="n">geom_poly</span><span class="p">)</span>
</pre></div>
</div>
<img alt="_images/quarter6.png" src="_images/quarter6.png" />
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Create centroids of each intersected polygon</span>
<span class="n">centroidTopLeft</span> <span class="o">=</span> <span class="n">quaterPolyTopLeft</span><span class="o">.</span><span class="n">Centroid</span><span class="p">()</span>
<span class="n">centroidTopRight</span> <span class="o">=</span>  <span class="n">quaterPolyTopRight</span><span class="o">.</span><span class="n">Centroid</span><span class="p">()</span>
<span class="n">centroidBottomLeft</span> <span class="o">=</span>  <span class="n">quaterPolyBottomLeft</span><span class="o">.</span><span class="n">Centroid</span><span class="p">()</span>
<span class="n">centroidBottomRight</span> <span class="o">=</span>  <span class="n">quaterPolyBottomRight</span><span class="o">.</span><span class="n">Centroid</span><span class="p">()</span>
</pre></div>
</div>
<img alt="_images/quarter9.png" src="_images/quarter9.png" />
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Geometry</a><ul>
<li><a class="reference internal" href="#create-a-point">Create a Point</a></li>
<li><a class="reference internal" href="#create-a-linestring">Create a LineString</a></li>
<li><a class="reference internal" href="#create-a-polygon">Create a Polygon</a></li>
<li><a class="reference internal" href="#create-a-polygon-with-holes">Create a Polygon with holes</a></li>
<li><a class="reference internal" href="#create-a-multipoint">Create a MultiPoint</a></li>
<li><a class="reference internal" href="#create-a-multilinestring">Create a MultiLineString</a></li>
<li><a class="reference internal" href="#create-a-multipolygon">Create a MultiPolygon</a></li>
<li><a class="reference internal" href="#create-a-geometrycollection">Create a GeometryCollection</a></li>
<li><a class="reference internal" href="#create-geometry-from-wkt">Create Geometry from WKT</a></li>
<li><a class="reference internal" href="#create-geometry-from-geojson">Create Geometry from GeoJSON</a></li>
<li><a class="reference internal" href="#create-geometry-from-gml">Create Geometry from GML</a></li>
<li><a class="reference internal" href="#create-geometry-from-wkb">Create Geometry from WKB</a></li>
<li><a class="reference internal" href="#count-points-in-a-geometry">Count Points in a Geometry</a></li>
<li><a class="reference internal" href="#count-geometries-in-a-geometry">Count Geometries in a Geometry</a></li>
<li><a class="reference internal" href="#iterate-over-geometries-in-a-geometry">Iterate over Geometries in a Geometry</a></li>
<li><a class="reference internal" href="#iterate-over-points-in-a-geometry">Iterate over Points in a Geometry</a></li>
<li><a class="reference internal" href="#buffer-a-geometry">Buffer a Geometry</a></li>
<li><a class="reference internal" href="#calculate-envelope-of-a-geometry">Calculate Envelope of a Geometry</a></li>
<li><a class="reference internal" href="#calculate-the-area-of-a-geometry">Calculate the Area of a Geometry</a></li>
<li><a class="reference internal" href="#calculate-the-length-of-a-geometry">Calculate the Length of a Geometry</a></li>
<li><a class="reference internal" href="#get-the-geometry-type-as-a-string-from-a-geometry">Get the geometry type (as a string) from a Geometry</a></li>
<li><a class="reference internal" href="#calculate-intersection-between-two-geometries">Calculate intersection between two Geometries</a></li>
<li><a class="reference internal" href="#calculate-union-between-two-geometries">Calculate union between two Geometries</a></li>
<li><a class="reference internal" href="#write-geometry-to-geojson">Write Geometry to GeoJSON</a></li>
<li><a class="reference internal" href="#write-geometry-to-wkt">Write Geometry to WKT</a></li>
<li><a class="reference internal" href="#write-geometry-to-kml">Write Geometry to KML</a></li>
<li><a class="reference internal" href="#write-geometry-to-wkb">Write Geometry to WKB</a></li>
<li><a class="reference internal" href="#force-polygon-to-multipolygon">Force polygon to multipolygon</a></li>
<li><a class="reference internal" href="#quarter-polygon-and-create-centroids">Quarter polygon and create centroids</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="gdal_general.html"
                        title="previous chapter">GDAL/OGR General</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="vector_layers.html"
                        title="next chapter">Vector Layers</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/geometry.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="vector_layers.html" title="Vector Layers"
             >next</a> |</li>
        <li class="right" >
          <a href="gdal_general.html" title="GDAL/OGR General"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Python GDAL/OGR Cookbook 1.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2013, Jared Erickson, Cort Daniel, Michael Payne.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
    </div>
  </body>
</html>